package com.kikt.rxjavaandretrofit

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.kikt.rxjavaandretrofit.bean.ResultsItem
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.reflect.InvocationHandler

class MainActivity : AppCompatActivity(), MainView {

    var mainPresenter: MainPresenter = MainPresenter(this)
    val optionsAdapter = OptionsAdapter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_date.setOnClickListener {
            mainPresenter.getRandom()
        }

        spinner.adapter = optionsAdapter
//        spinner.adapter =

        category.setOnClickListener {
            val item = optionsAdapter.getItem(spinner.selectedItemPosition)
            mainPresenter.getCategory(item)
        }
    }

    override fun onUpdateData(results: List<ResultsItem?>?) {
        results?.let {
            val intent = Intent(this, ImageActivity::class.java)
            val toTypedArray = it.toTypedArray()
            intent.putExtra("list", toTypedArray)
            startActivity(intent)

        }


    }


}

class OptionsAdapter(val context: Context) : BaseAdapter() {

    val array = arrayOf("福利", "Android", "iOS", "休息视频", "拓展资源", "前端", "all")

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val item = getItem(position)
        val textView = TextView(context)
        textView.text = item
        return textView
    }

    override fun getItem(position: Int): String {
        return array[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return array.size
    }

}

fun InvocationHandler.logInfo(msg: String) {
    Log.i(this.javaClass.simpleName, msg)
}
